#!/bin/sh
########################################################################
# Begin $rc_base/init.d/swconfig
#
# Description : Script to setup lan switch.
#               don't edit this script! If you want change the functions
#               create an own script called swconfig.user
########################################################################

. /etc/sysconfig/rc
. ${rc_functions}

if [ -e /etc/init.d/swconfig.user ]; then
	/etc/init.d/swconfig.user $*
	exit ${?}
fi

if [ -e /var/ipfire/ethernet/swconfig_mac ]; then
SWMAC=`cat /var/ipfire/ethernet/swconfig_mac`
else
# Generate a random local administrated mac address for vlan swconfig.
SWMAC=`printf "%1x2:%02x:%02x:%02x:%02x" $[RANDOM%16] $[RANDOM%256] $[RANDOM%256] $[RANDOM%256] $[RANDOM%256]`
echo $SWMAC > /var/ipfire/ethernet/swconfig_mac
fi

case "${1}" in
	start)
		case `cat /proc/device-tree/model 2>/dev/null` in
			"Lamobo-R1")
				#
				# Lamobo R1 aka BPi R1 Routerboard
				#
				# Speaker | LAN1 | LAN2 | LAN3 | LAN4 || LAN5 | HDMI
				# SW-Port |  P2  |  P1  |  P0  |  P4  ||  P3  |
				# VLAN    |  11  |  12  |  13  |  14  ||ALL(t)|
				#
				# Switch-Port P8 - ALL(t) boards internal CPU Port
				#
				device=`ls /sys/class/net/*/device/stmmac-0* | head -1 | cut -d/ -f5`
				ip link set $device up
				boot_mesg "Configure vlan-switch on $device ..."
				# Reset switch, counter and enable vlan mode
				swconfig dev $device set reset 1
				swconfig dev $device set reset_mib 1
				swconfig dev $device set enable_vlan 1
				# configure vlans
				swconfig dev $device vlan 11 set ports "2 3t 8t"
				swconfig dev $device vlan 12 set ports "1 3t 8t"
				swconfig dev $device vlan 13 set ports "0 3t 8t"
				swconfig dev $device vlan 14 set ports "4 3t 8t"
				# activate new config
				swconfig dev $device set apply 1
				# create interfaces for the vlan's
				modprobe 8021q
				vconfig add $device 11
				vconfig add $device 12
				vconfig add $device 13
				vconfig add $device 14
				# set local mac addresses.
				ip link set dev $device.11 address $SWMAC:11
				ip link set dev $device.12 address $SWMAC:12
				ip link set dev $device.13 address $SWMAC:13
				ip link set dev $device.14 address $SWMAC:14
				# need to restart udev...
				killall udevd
				/etc/init.d/udev start
			;;
		esac
		exit 0
	;;

	*)
		echo "Usage: ${0} {start}"
		exit 1
	;;
esac

# End $rc_base/init.d/swconfig

